package com.itheima.count.controller.Impl;

import com.itheima.count.controller.ICountController;
import com.itheima.count.service.ICountService;
import com.itheima.domain.Student;
import com.itheima.factory.Factory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class CountControllerImpl implements ICountController {

    ICountService countService = Factory.getICountService();

    public CountControllerImpl() throws Exception {
    }


    @Override
    public int getAgeBoy(ArrayList<Student> list) {

        long count = list.stream().filter(s -> s.getBirthday().getYear() >= 1900 && s.getBirthday().getYear() < 2000).count();

        return (int) count;
    }

    @Override
    public void getReprot() throws IOException, ClassNotFoundException {
        ArrayList<Student> list = countService.getlist();
        System.out.println("90后有"+getAgeBoy(list)+"人");
        System.out.println("00后有"+getAge(list)+"人");
        System.out.println("男生有"+getMan(list)+"人");
        System.out.println("女生有"+getGirl(list)+"人");

        System.out.println("总人数有"+getAll(list)+"人");
        HashMap<Integer, Integer> avgage = getAvgage(list);
        avgage.forEach((k,v)-> System.out.println(k+"组的平均年龄"+v));

        System.out.println("手机号有俩6的有"+getPhone(list)+"人");
        HashMap<String, Integer> getxing = getxing(list);
        getxing.forEach((k,v)-> System.out.println(k+"姓有"+v+"人"));



    }

    @Override//统计男生
    public int getMan(ArrayList<Student> list) {
        long count = list.stream().filter(s -> s.getSex() == "男").count();
        // System.out.println("男生有"+count+"ren");
        return (int) count;

    }

    @Override//统计女生
    public int getGirl(ArrayList<Student> list) {
        long count = list.stream().filter(s -> s.getSex() == "女").count();
        // System.out.println("女生有"+count+"ren");
        return (int) count;

    }

    @Override//统计各年龄段的人数00后
    public int getAge(ArrayList<Student> list) {
        long count = list.stream().filter(s -> s.getBirthday().getYear() >= 2000).count();
        // System.out.println("00后有"+count+"人");
        // int count1 = list.stream().filter(s -> s.getshengri.getYear() >= 1900).count();
        // System.out.println("00后有"+count1+"人");


        return (int) count;

    }





    @Override//统计总人数
    public int getAll(ArrayList<Student> list) {

        // System.out.println(list.size());
        return list.size();
    }

    @Override //统计平均年龄
    public HashMap<Integer,Integer> getAvgage(ArrayList<Student> list) {
        HashMap<Integer,ArrayList<Student>> map = new HashMap<>();
        for (Student student : list) {

            int group =student.getGroup() ;
            if (map.containsKey(group)){
                map.get(group).add(student);
            }else {
                ArrayList<Student> people = new ArrayList<>();
                people.add(student);
                map.put(group,people);
            }
        }

        HashMap<Integer, Integer> map1 = new HashMap<>();
        Set<Integer> integers = map1.keySet();
        for (Integer integer : integers) {
            ArrayList<Student> integer1 = map.get(integer);
            int sum=0;
            for (int i = 0; i < integer1.size(); i++) {
                int age = integer1.get(i).getAge();
                sum+=age;
            }

            int avg=sum/integer1.size();
            map1.put(integer,avg);

        }
        return map1;
    }



    @Override//手机号有俩6
    public int getPhone(ArrayList<Student> list) {
        long count = list.stream().filter(s -> s.getPhone().split("6").length > 2).count();
        return (int) count;
    }

    //每个姓有多少人
    public HashMap<String, Integer> getxing(ArrayList<Student> list) {
        HashMap<String, ArrayList<Student>> map = new HashMap<>();
        for (Student student : list) {
            String xing = student.getName().substring(0, 1);
            if (map.containsKey(xing)) {
                map.get(xing).add(student);
            } else {
                ArrayList<Student> students = new ArrayList<>();
                students.add(student);
                map.put(xing, students);
            }
        }

        HashMap<String, Integer> xingMap = new HashMap<>();
        Set<String> xings = map.keySet();
        for (String xing : xings) {
            ArrayList<Student> students = map.get(xing);
            xingMap.put(xing, students.size());
        }
        return  xingMap;
    }

}
